(function($){
	$(document).ready(function(){
		$("#id_app").css('display', 'none');
		$("#lookup_id_app").after("&nbsp;&nbsp;&nbsp;<a target='_blank' href='##' id='id_app_name'></a>");
		$("#id_app_name").parent().append("<span class='help-block'><strong><i>Click the 'magnifying glass' icon to choose an app</i></strong></span>");

		$("#id_local_doc").bind('input', function(){
			if($(this).val() != ""){
				var local = $(this).val().replace(/\\/g, '/');
				if(local.indexOf(":") > -1){
					var prefix = local.split(":", 1);
					$("#id_remote_doc").val(local.replace(prefix + ":", "https://www.dropbox.com/home/Small Tree Media/Project Submission Doc"));
				}else{
					var prefix = local.split("/Project Submission Doc/", 1);
					$("#id_remote_doc").val(local.replace(prefix, 'https://www.dropbox.com/home/Small Tree Media'));
				}
			}else{
				$("#id_remote_doc").val("");
			}
		});
		
		$("td.field-type > input").css('display', 'none');
		$("td.field-type > input").after("\
	<select id='iap_type_select'>\
		<option>---------</option> \
	</select>");
		
		if($("#id_app").val() != ""){
			$.getJSON("/get_iap_type/", {'id': $("#id_app").val()}, function(data){
	    		if(data.res == ''){
	    			$("td.field-type > select").css('display', 'none');
	    			$("td.field-type > input").css('display', 'block');
	    		}else{
	    			$("td.field-type > select").css('display', 'block');
	    			$("td.field-type > input").css('display', 'none');
	        		var html = "<option value=''>---------</option>";
	        		$.each(data.res, function(k, v){
	        			html += "<option value='" + v + "'>" + v + "</option>";
	        		})
	        		$("td.field-type > select").html(html);
	    		}
				$("td.field-type > input").each(function(){
					$(this).parent().children("select").val($(this).val());
				});
	    	});
		}

		$("td.field-type > select").live('change', function(e){
			$(this).parent().children($("input.vTextField")).val(e.target.value);
		})
	
	});
})(django.jQuery);
